﻿using System;
using System.Collections.Generic;
using System.Text;
using YouHP.Utility;
using YouHP.BLL;
using YouHP.Model;
using YouHP.Model.Group;
using System.Web;

namespace YouHP.Pages.BBS
{
    public class TopicListBySearch : BoardBasePage
    {

        protected List<TopicInfo> TopicDataList = new List<TopicInfo>();
        protected TopicSearchCondition cond = new TopicSearchCondition();
        protected int PageCount;
        protected int PageIndex;
        protected int PageSize = 20;
        protected string PagingString = "";
        protected int DataCount = 0;

        protected override void ShowPage()
        {
            base.ShowPage();
            if (ErrorCount > 0) return;

            HandleCommon();
            if (ErrorCount > 0) return;

            if (IsPost) HandlePost();
            else HandleGet();


        }
        protected void HandleCommon()
        {
            this.AddLocation("帖子检索结果", "");
        }
        protected void HandlePost()
        {

            int actiontype = RequestTools.GetFormInt("actiontype", -1);
            string TopicSort = RequestTools.GetFormString("TopicSort");

            int[] idArray = Utils.StrToIntArray(RequestTools.GetFormString("topicid"), ",");

            string CurrentUrl = HttpContext.Current.Request.Url.ToString();

            bool succ = false;
            switch (actiontype)
            {
                case 1://置顶帖子

                    succ = TopicBLL.Instance.UpdateTopicLevel(GroupData.Id, idArray, Constants.TopicGroupTop);
                    if (succ)
                    {
                        this.AddMsgLine("操作成功!");
                        this.MsgboxUrl = CurrentUrl;
                        this.SetMetaRefresh();
                    }
                    else
                    {
                        this.AddErrLine("操作失败!");
                        return;
                    }
                    break;
                case 2: //加精帖子
                    succ = TopicBLL.Instance.UpdateBestTopic(GroupData.Id, idArray, 1);

                    if (succ)
                    {
                        this.AddMsgLine("操作成功!");
                        this.MsgboxUrl = CurrentUrl;
                        this.SetMetaRefresh();
                    }
                    else
                    {
                        this.AddErrLine("操作失败!");
                        return;
                    }
                    break;
                case 3: //取消置顶
                    succ = TopicBLL.Instance.UpdateTopicLevel(GroupData.Id, idArray, Constants.TopicGeneral);
                    if (succ)
                    {
                        this.AddMsgLine("操作成功!");
                        this.MsgboxUrl = CurrentUrl;
                        this.SetMetaRefresh();
                    }
                    else
                    {
                        this.AddErrLine("操作失败!");
                        this.SetShowBackLink(true);
                        return;
                    }
                    break;
                case 4: //取消加精
                    succ = TopicBLL.Instance.UpdateBestTopic(GroupData.Id, idArray, 0);
                    if (succ)
                    {
                        this.AddMsgLine("操作成功!");
                        this.MsgboxUrl = CurrentUrl;
                        this.SetMetaRefresh();
                    }
                    else
                    {
                        this.AddErrLine("操作失败!");
                        return;
                    }
                    break;
                case 5://设置帖子标题颜色(绿色)
                    succ = TopicBLL.Instance.UpdateTitleColor(GroupData.Id, idArray, Constants.Color_Green);
                    if (succ)
                    {
                        this.AddMsgLine("操作成功!");
                        this.MsgboxUrl = CurrentUrl;
                        this.SetMetaRefresh();
                    }
                    else
                    {
                        this.AddErrLine("操作失败!");
                        return;
                    }
                    break;
                case 6://设置帖子标题颜色(红色)
                    succ = TopicBLL.Instance.UpdateTitleColor(GroupData.Id, idArray, Constants.Color_Red);
                    if (succ)
                    {
                        this.AddMsgLine("操作成功!");
                        this.MsgboxUrl = CurrentUrl;
                        this.SetMetaRefresh();
                    }
                    else
                    {
                        this.AddErrLine("操作失败!");
                        return;
                    }
                    break;
                case 7://取消标题颜色高亮显示
                    succ = TopicBLL.Instance.UpdateTitleColor(GroupData.Id, idArray, Constants.Color_General);
                    if (succ)
                    {
                        this.AddMsgLine("操作成功!");
                        this.MsgboxUrl = CurrentUrl;
                        this.SetMetaRefresh();
                    }
                    else
                    {
                        this.AddErrLine("操作失败!");
                        return;
                    }
                    break;


                case 0://删除帖子
                    foreach (int tid in idArray)
                    {
                        int uid = TopicBLL.Instance.GetTopicInfo(tid).PostUserId;
                        succ = TopicBLL.Instance.DeleteTopic(GroupData.Id, tid, uid);
                        if (!succ) break;
                    }
                    if (succ)
                    {
                        this.AddMsgLine("操作成功！");
                        this.MsgboxUrl = CurrentUrl;
                        this.SetMetaRefresh();
                    }
                    else
                    {
                        this.AddErrLine("操作失败!");
                        return;
                    }

                    break;
            }

            succ = TopicBLL.Instance.UpdateTopicSort(GroupData.Id, idArray, TopicSort);
            if (succ)
            {
                this.AddMsgLine("操作成功!");
                this.SetUrl("/BBS/Board.aspx?gid=" + GroupData.Id);
                this.SetMetaRefresh();
            }
            else
            {
                this.AddErrLine("操作失败!");
                return;
            }


        }
        protected void HandleGet()
        {
            string title = RequestTools.GetQueryString("title").Trim();
            string author = RequestTools.GetQueryString("author").Trim();

            if (author.Length != 0)
            {
                UserInfo info = UserBLL.Instance.GetUserInfo(author);
                if (info.Id < 0)
                {
                    this.AddErrLine("您输入的用户不存在！");
                    return;
                }
                cond.AuthorId = info.Id;
                cond.AuthorName = info.UserName;
            }

            cond.Title = title;


            PageIndex = RequestTools.GetQueryInt("page", 1);


            TopicDataList = TopicBLL.Instance.GetTopicListByCond(GroupData.Id, cond, PageIndex, PageSize, out DataCount);

            PageCount = Utils.GetPageCount(DataCount, PageSize);

            string query = HttpContext.Current.Request.Url.Query;
            query = Utils.RemoveQueryItem(query, "page");

            PagingString = this.GetPagingString(
                PageIndex,
                PageCount,
                "/BBS/TopicListBySearch.aspx" + query, "page");
        }

        //判断是否版主
        protected bool IsModerator()
        {
            bool retVal = false;
            for (int i = 0; i < moderatorsId.Length; i++)
            {
                if (UserData.Id == moderatorsId[i])
                {
                    retVal = true;
                    break;
                }
            }
            return retVal;
        }
    }
}
